<include file='Index/header' />
<div class="row">
	<br>
	<div class="col-sm-12">
		<div class="col-sm-2"></div>
		<div class="col-sm-1" style="padding-top:5px;">
			<label class="control-label">日期：</label>
		</div>
		<div class="col-sm-3">
			<input id="start_date" class="form-control"></input>
		</div>
		<div class="col-sm-1" style="padding-top:5px;">
			<label for="barcode" class="control-label">货号：</label>
		</div>
		<div class="col-sm-3">
			<input id="barcode" name="barcode" class="form-control"></input>
		</div>
		<div class="col-sm-2">
			<button class="btn btn-primary" onclick="print()">打印</button>
		</div>
	</div>
	<div class="col-sm-12">
		<br>
		<div class="col-sm-2">
			<table>
				<tr>请选择仓库：</tr>
				<tr><ul id="repository_tree" class="ztree"></ul></tr>
				<br>
				<tr>请选择商品类别：</tr>
				<tr><ul id="product_class_tree" class="ztree"></ul></tr>
			</table>
		</div>
		<div class="col-sm-10">
			<table id="table" class="table table-hover table-bordered table-striped table-striped">
				<caption id="repository" style="text-align:center"></caption>
				<thead>
					<tr>
						<td>大类</td>
						<td>中类</td>
						<td>子类</td>
						<td>A仓库</td>
						<td>B仓库</td>
						<td>编号</td>
						<td>商品名称</td>
						<td>条形码货号</td>
						<td>价格</td>
						<td>补货量</td>
						<td>统计日期</td>
					</tr>
				</thead>
				<tbody>

				</tbody>
			</table>
		</div>
	</div>
</div>
<include file='Index/footer' />
<script>
$(document).ready(function() {
	loadDatetimepicker('start_date');
	loadRepositoryTree('repository_tree', true);
	loadProductClassTree('product_class_tree', true, false);
	loadTable();
});


var table;
function loadTable(){
	// var data = '{$data}';
	// data = eval(data);

	table = $('#table').DataTable({
		"bProcessing": true,
		"bDestroy": true,
		"deferRender": true,
		"sPaginationType" : "input",
		"oLanguage" : {
              "sLengthMenu": "每页显示 _MENU_ 条记录",
              "sZeroRecords": "抱歉， 没有找到",
              "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
              "sInfoEmpty": "没有数据",
              "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
              "sZeroRecords": "没有检索到数据",
               "sSearch": "搜索: &nbsp",
              "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "前一页",
                "sNext": "后一页",
                "sLast": "尾页"
              }
          }, 
    "serverSide": true,
		"ajax":'{:U("Admin/statistic/getabRepositorySupply/")}',
		"columns": [
					{ "data": 'class1' },
					{ "data": 'class2' },
					{ "data": 'class3' },
					{ "data": 'a_repository_id' },
					{ "data": 'b_repository_id' },
					{ "data": 'id' },
					{ "data": 'name' },
	        { "data": 'barcode' },
	        { "data": 'kj_price' },
	        { "data": 'supply_amount' },
	        { "data": 'createtime' }
    ],
    "columnDefs": [
    	{
				"targets":[0,1,2,3,4],
				"visible":false
			},
			{
				"targets":5,
				"render": function (data, type, row, table) {
	    		return table.row + 1;
	      }
			}
   ]
	});	

	$('#start_date').datetimepicker().on('changeDate', function(e){
    table.column(-1).search(this.value).draw();
  });
  $("#barcode").on( 'keyup', function () {
    table.column(7).search(this.value).draw();
  });
}


function searchARepository(val){
	table.column(3).search('').draw();
	table.column(4).search('').draw();
	table.column(3).search(val).draw();
}

function searchBRepository(val){
	table.column(3).search('').draw();
	table.column(4).search('').draw();
	table.column(4).search(val).draw();
}

function searchClass1(product_class_id){
	table.column(0).search('').draw();
	table.column(1).search('').draw();
	table.column(2).search('').draw();
  table.column(0).search(product_class_id).draw();
}
function searchClass2(product_class_id){
	table.column(0).search('').draw();
	table.column(1).search('').draw();
	table.column(2).search('').draw();
  table.column(1).search(product_class_id).draw();
}
function searchClass3(product_class_id){
	table.column(0).search('').draw();
	table.column(1).search('').draw();
	table.column(2).search('').draw();
  table.column(2).search(product_class_id).draw();
}

function print(){
	var treeObj = $.fn.zTree.getZTreeObj("repository_tree");
  var nodes = treeObj.getSelectedNodes();
  if(nodes.length == 0){
      alert("请选择一个仓库！");
      return;
  }

	var a_repository_id='',b_repository_id='';
  if(typeof(nodes[0].a_repository_id) == 'undefined'){
		b_repository_id = nodes[0].b_repository_id;
		$.ajax({
			url: '{:U("Admin/Repository/getBRepository/")}',
			type: 'post',
			dataType: 'json',
			data:{
				"b_repository_id":b_repository_id
			}
		})
		.success(function(data){
			printpage(data.name);
		});
	}else{
		a_repository_id = nodes[0].a_repository_id;
		$.ajax({
			url: '{:U("Admin/Repository/getARepository/")}',
			type: 'post',
			dataType: 'json',
			data:{
				"a_repository_id":a_repository_id
			}
		})
		.success(function(data){
			printpage(data.name);
		});
	}
}

function printpage(name){
	//$('#table').attr('style', 'pageBreakAfter:"always"');
	var len = $("select[name='table_length']").val();
	table.page.len( -1 ).draw();
	$('#repository').html(name+"补货单");
	$("#table").jqprint({
    debug: false, //如果是true则可以显示iframe查看效果（iframe默认高和宽都很小，可以再源码中调大），默认是false
    importCSS: true, //true表示引进原来的页面的css，默认是true。（如果是true，先会找$("link[media=print]")，若没有会去找$("link")中的css文件）
    printContainer: true, //表示如果原来选择的对象必须被纳入打印（注意：设置为false可能会打破你的CSS规则）。
    operaSupport: true//表示如果插件也必须支持歌opera浏览器，在这种情况下，它提供了建立一个临时的打印选项卡。默认是true
	});
	table.page.len(len).draw();
}
</script>